<template>
  <VPage>
    <template #left>
      <Type ref="TypeRef" @change="typeChange" />
    </template>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VCascader v-model="form.create_ids" placeholder="创建人" multiple collapse-tags style="min-width: 160px" />
        <el-input v-model="form.value" placeholder="请输入名称" class="w-60" />
      </template>
      <template #toolbar_btns>
        <VButton auth="/base/label/add-label" type="primary" icon="DocumentAdd" @click="create(tableRef)">添加</VButton>
        <VButton auth="/base/label/delete-label" @click="handleBatch('delete')">删除</VButton>
      </template>
    </VTable>
    <Add ref="addRef" :options="options" :lt_id="lt_id" @success="query" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import Type from './components/Type/index'
import { getOption } from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus'

const baseName = 'brand'
const tableRef = ref()
const loadings = reactive({})
const lt_id = ref(0)
// 查询
const query = () => {
  tableRef?.value?.query()
}

// 类型变化
const typeChange = (val) => {
  lt_id.value = val
  tableRef?.value?.setFormField('lt_id', val)
  query()
}

// 新增
const addRef = ref()
const create = () => {
  addRef?.value.open()
}

// 获取options
const options = ref({})
Promise.all([
  getOption({ type: baseName }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
})


// 批量操作
const handleBatch = (name) => {
  const selection = tableRef.value.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const label_ids = selection.map(d => d.label_id)
  const showLoading = () => loadings[name] = true
  const closeLoading = () => loadings[name] = false
  const handle = {
    delete: () => {
      ElMessageBox.confirm('是否确认删除？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          showLoading()
          api.delete({ label_ids }).then(res => {
            ElMessage({ type: 'success', message: '操作成功', })
            query()
          }).finally(closeLoading)
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })
    },
  }
  handle[name]()
}
// 单行操作
const handleRow = (name, row, rowIndex) => {
  const { label_id } = row
  const showLoading = (name = 'handle') => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = (name = 'handle') => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    edit: () => {
      addRef?.value.open(label_id)
    },
    delete: () => {
      ElMessageBox.confirm('是否确认删除？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          showLoading()
          api.delete({ label_ids: [label_id] }).then(res => {
            ElMessage({ type: 'success', message: '操作成功', })
            query()
          }).finally(closeLoading)
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  showHeaderOverflow: false,
  formConfig: {
    data: {
      input: { type: 'brand_name', value: '' }, // 组合
      dateRange: { type: 'deadline_date', value: [], range: true }, // 组合
    }
  },
  columns: [
    { type: 'checkbox', fixed: 'left', width: 40 },
    {
      title: '标签名称', field: 'name', minWidth: 200, line: 2
    },
    { title: '标签类型', field: 'type_name', minWidth: 100, slots: {
      default: ({row}) => {
        console.log('row.type_name', row.type_name)
        return <VText value={row.type_name} />
      }
    } },
    { title: '标签条目', field: 'count', minWidth: 100 },
    { title: '创建人', field: 'create_name', minWidth: 140 },
    { title: '最后编辑人', field: 'update_name', minWidth: 150 },
    { title: '创建时间', field: 'create_time', minWidth: 170 },
    { title: '更新时间', field: 'update_time', minWidth: 170 },
    {
      title: '操作', width: 110, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          return <div>
            <VButton auth="/base/label/update-label" link type="primary" onClick={() => handleRow('edit', row)}>编辑</VButton>
            <VButton auth="/base/label/delete-label" link type="danger" onClick={() => handleRow('delete', row, rowIndex)}>删除</VButton>
          </div>
        }
      }
    }
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  